﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Core.DAL.Repositories;
using Medianamik.Core.DAL.Services;
using Medianamik.UI.Web.Shop.Data.Interfaces;
using SubSonic;

namespace Medianamik.UI.Web.Shop.Data.Repositories
{
    public class ShoppingCartRepository<TCollection, T, TItem, TModel> : 
        MedianamikRepository<TCollection, T>, 
        IShoppingCartRepository<T, TItem, TModel>
        where T : IShoppingCart<TItem, TModel>, IRecordBase, new()
        where TCollection : ICollectionWithChildren<TCollection, T>, new() 
        where TModel : Interfaces.IModel 
        where TItem : IShoppingCartItem<TModel>
    {
        public ShoppingCartRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper) : base(cultureService, contentStateService, nodeMapper)
        {
        }

        public override T Get(Guid nodeId)
        {
            return Get(nodeId, CultureInfo.InvariantCulture, ContentState.Draft);
        }

        public virtual T GetShoppingCartByOrderId(Guid orderId)
        {
            var wheres = new List<Where>
                 {
                     new Where
                         {
                             ColumnName = ShopSchema.ShoppingCart.Columns.OrderId,
                             Comparison = Comparison.Equals,
                             ParameterValue = orderId
                         }
                 };

            return GetWithFilters(CultureInfo.InvariantCulture, ContentState.Draft, ActiveStatus.All,
                TypeIds.ShoppingCart, null, wheres, null, "", null, true).FirstOrDefault();
        }
    }

    public class ShoppingCartRepository :
        ShoppingCartRepository<ShoppingCartCollection, ShoppingCart, ShoppingCartItem, Model>, IShoppingCartRepository
    {
        public ShoppingCartRepository(ICultureService cultureService, IContentStateService contentStateService, INodeMapper nodeMapper)
            : base(cultureService, contentStateService, nodeMapper)
        {
        }
    }

    public interface IShoppingCartRepository<T, TItem, TModel> : IMedianamikRepository<T>
        where T : IShoppingCart<TItem, TModel> 
        where TModel : Interfaces.IModel 
        where TItem : IShoppingCartItem<TModel>
    {
        T GetShoppingCartByOrderId(Guid orderId);
    }

    public interface IShoppingCartRepository: IShoppingCartRepository<ShoppingCart, ShoppingCartItem, Model>
    {}
}
